/**
 * 限制Message显示频率
 * @author 木人心
 * @version 1.0.0
 */
import { ElMessage } from 'element-plus'
import { messageConfig } from '@utils/request/config'
import { FrequencyMessageType } from '@utils/request/type'

// 限制相同的提示内容不要提示过于频繁
const messageHistory = new Set()
export const FrequencyMessage = function (message, type) {
  if (!messageHistory.has(message)) {
    messageHistory.add(message)
    ElMessage[type]({ message, showClose: true })
    setTimeout(() => {
      messageHistory.delete(message)
    }, messageConfig.frequency)
  }
} as FrequencyMessageType

['warning', 'error', 'info'].forEach(type => {
  FrequencyMessage[type] = (message: string) => {
    FrequencyMessage(message, type)
  }
})
